package cn.ffcs.uec.organization.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;

import cn.ffcs.uec.organization.component.bean.UecOrgDeptsListboxExtBean;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.UecOrgDeptVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOrgDeptsListboxExt extends Div implements IdSpace {
	private static final long serialVersionUID = -648725659643923129L;

	private UecOrgDeptsListboxExtBean bean = new UecOrgDeptsListboxExtBean();
	/**
	 * zul.
	 */
	private static final String zul = "/pages_nmg/uec_organization/comp/uecOrgDepts_listbox_ext.zul";

	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	private UecOrgDeptVo uecOrgDeptVo;

	private UecOrgOrgs uecOrgOrgs;

	// 区别盟市间，市内调配
	@Getter
	@Setter
	private String crossOrgFlag;
	
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOrgDeptsListboxExt() {
		// 1.绑定页面
		Executions.createComponents(zul, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');
	}

	/**
	 * 触发选中事件
	 */
	public void onSelectRequest() throws Exception {
		if (bean.getUecOrgDeptsListbox().getSelectedCount() > 0) {
			uecOrgDeptVo = (UecOrgDeptVo) bean.getUecOrgDeptsListbox()
					.getSelectedItem().getValue();
			Events.postEvent("onSelectUecOrgDeptVo", this, uecOrgDeptVo);
		} else {
			Events.postEvent("onSelectNoUecOrgDeptVo", this, null);
		}
	}

	public void onSelectOrgDeptByPkOrgResponse(ForwardEvent event) {
		uecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
		onQuery();
	}

	/**
	 * 查询
	 */
	public void onQuery() {
		uecOrgDeptVo = new UecOrgDeptVo();
		uecOrgDeptVo.setOrgname(bean.getOrgname().getValue());
		uecOrgDeptVo.setPorg(uecOrgOrgs.getPkOrg());
		this.queryOrganization();
	}

	/**
	 * 查询
	 */
	public void queryOrganization() {
		int activePage = bean.getUecOrgDeptsListboxPaging().getActivePage();
		int pageSize = bean.getUecOrgDeptsListboxPaging().getPageSize();
		PageInfo pageInfo = uecOrgDeptManager.queryUecOrgDeptPage(uecOrgDeptVo,
				activePage + 1, pageSize);

		if (pageInfo != null) {
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getUecOrgDeptsListbox().setModel(dataList);
			bean.getUecOrgDeptsListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}else {
			cn.ffcs.uom.common.util.PubUtil.clearListbox(bean.getUecOrgDeptsListbox());
			bean.getUecOrgDeptsListboxPaging().setTotalSize(0);
		}
	}

	/**
	 * 重置
	 */
	public void onReset() {
		bean.getOrgname().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getUecOrgDeptsListbox());
		this.bean.getUecOrgDeptsListboxPaging().setTotalSize(1);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOrgDeptVo = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.queryOrganization();
	}

}
